/*
 * OpennavUtils.h
 *
 *  Created on: 24.02.2011
 *      Author: Markus
 */

#ifndef OPENNAVUTILS_H_
#define OPENNAVUTILS_H_

#include <FBase.h>
#include <FGraphics.h>
#include <FLocations.h>
#include <FText.h>
#include "Opennav.h"

class OpennavUtils {
public:
	static void Convert(const opennav::Coordinate& from, Osp::Locations::Coordinates& to) {
		to.Set(from.dlatitude(), from.dlongitude(), 0.0);
	}

	static void Convert(const Osp::Locations::Coordinates& from, opennav::Coordinate& to) {
		to = opennav::Coordinate(from.GetLongitude(), from.GetLatitude());
	}

	static void Convert(const opennav::Point& from, Osp::Graphics::Point& point) {
		point.SetPosition(from.x, from.y);
	}

	static void Convert(const Osp::Graphics::Point& from, opennav::Point& point) {
		point.x = from.x;
		point.y = from.y;
	}

	static Osp::Base::Collection::IList* Convert(const std::vector<opennav::Point>& from) {
		Osp::Base::Collection::ArrayList* converted = new Osp::Base::Collection::ArrayList();
		converted->Construct(from.size());
		for (std::vector<opennav::Point>::const_iterator it = from.begin(); it != from.end(); it++) {
			Osp::Graphics::Point* point = new Osp::Graphics::Point();
			Convert(*it, *point);
			converted->Add(*point);
		}
		return converted;
	}

	static void Convert(const opennav::Color& from, Osp::Graphics::Color& to) {
		to.SetColorComponents(from.r, from.g, from.b, 255);
	}

	static void Convert(const Osp::Graphics::Dimension& from, opennav::Dimension& to) {
		to.width = from.width;
		to.height = from.height;
	}

	static void Convert(const opennav::Font& from, Osp::Graphics::Font& to) {
		switch (from.style) {
		case opennav::FONT_PLAIN: to.Construct(Osp::Graphics::FONT_STYLE_PLAIN, from.size);  break;
		case opennav::FONT_BOLD: to.Construct(Osp::Graphics::FONT_STYLE_BOLD, from.size);break;
		case opennav::FONT_ITALIC: to.Construct(Osp::Graphics::FONT_STYLE_ITALIC, from.size);break;
		}
	}

//	static Osp::Base::String* Convert(const int8_t* from) {
//		return new Osp::Base::String((const char*)from);
//	}
//
//	static const int8_t* Convert(const Osp::Base::String* from) {
//		return (const int8_t*)from->GetPointer();
//	}

	static result Convert(const Osp::Base::String& from, std::string& to) {
		Osp::Text::AsciiEncoding ascii;
		int byteCount = 0;
		Osp::Base::ByteBuffer* pBuffer = ascii.GetBytesN(from);
		if (!pBuffer) return E_FAILURE;
		byteCount = pBuffer->GetCapacity();
		char* chPtrBuf = new char[byteCount + 1];
		pBuffer->GetArray((byte*)chPtrBuf, 0, byteCount);
		delete pBuffer;
		to = chPtrBuf;
		if (chPtrBuf != null) delete [] chPtrBuf;
		return E_SUCCESS;
	}

	static result Convert(const std::string& from, Osp::Base::String& to) {
		to = Osp::Base::String(from.data());
		return E_SUCCESS;
	}
};

#endif /* OPENNAVUTILS_H_ */
